package com.warm.pump.module.skd.service.impl;

import com.warm.pump.executor.InstanceService;
import com.warm.pump.frame.gen.bean.PageBean;
import com.warm.pump.frame.util.DateUtils;
import com.warm.pump.frame.util.IpUtil;
import com.warm.pump.module.skd.bean.po.gen.ScheduleInstance;
import com.warm.pump.module.skd.bean.po.gen.ScheduleInstanceDtl;
import com.warm.pump.module.skd.dao.gen.ScheduleInstanceDao;
import com.warm.pump.module.skd.dao.gen.ScheduleInstanceDtlDao;
import com.warm.pump.module.skd.service.IScheduleInstanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("ScheduleInstanceService")
public class ScheduleInstanceServiceImpl implements IScheduleInstanceService {
    @Autowired
    private ScheduleInstanceDao scheduleInstanceDao;
    @Autowired
    private ScheduleInstanceDtlDao scheduleInstanceDtlDao;

    @Value("${server.port}")
    private Integer port;
    @Autowired
    private InstanceService instanceService;

    @Override
    public ScheduleInstance queryObject(Long id) {
        return scheduleInstanceDao.selectByPrimaryKey(id);
    }

    public ScheduleInstance queryObject(Map<String, Object> map) {
        return scheduleInstanceDao.selectByMap(map);
    }

    @Override
    public List<ScheduleInstance> queryList(Map<String, Object> map) {
        return scheduleInstanceDao.getListByMap(map);
    }

    @Override
    public PageBean getPageBeanByMap(Map map, int pageNo, int pageSize) {
        return scheduleInstanceDao.getPageBeanByMap(map, pageNo, pageSize);
    }


    @Override
    public int queryTotal(Map<String, Object> map) {
        return scheduleInstanceDao.countTotalByMap(map);
    }

    @Override
    public void save(ScheduleInstance scheduleInstance) {
        scheduleInstanceDao.insertSelective(scheduleInstance);
    }

    @Override
    public void update(ScheduleInstance scheduleInstance) {
        scheduleInstanceDao.updateByPrimaryKeySelective(scheduleInstance);
    }

    @Override
    public void delete(Long id) {
        scheduleInstanceDao.deleteByPrimaryKey(id);
    }

    @Override
    public void delete(Map<String, Object> map) {
        scheduleInstanceDao.deleteByMap(map);
    }

    @Override
    public void deleteBatch(Long[] ids) {
        Map<String, Object> map = new HashMap<>();
        map.put("id_in", ids);
        scheduleInstanceDao.deleteByMap(map);
    }

    @Override
    public void run(Long[] ids) {
        for (Long id : ids) {
            this.createInstByInstId(id);
        }
    }


    /**
     * 执行实例立即执行
     *
     * @param id
     */
    @Transactional
    protected void createInstByInstId(Long id) {
        ScheduleInstance instOld = scheduleInstanceDao.selectByPrimaryKey(id);
        String instanceId = instOld.getInstanceId();
        String newInstanceId = DateUtils.getDataString(DateUtils.yyyymmddhhmmss) + "33" + instOld.getId();
        ScheduleInstance inst = new ScheduleInstance();
        inst.setId(null);
        inst.setReferType(instOld.getReferType());
        inst.setReferId(instOld.getReferId());
        inst.setReferName(instOld.getReferName());
        inst.setStatus(1);
        inst.setInstanceId(newInstanceId);
        inst.setRemark("执行实例立即执行，关联instanceId为:" + instanceId);
        inst.setUpdateTime(new Date());
        inst.setCreateTime(new Date());
        inst.setCreateIp(IpUtil.getIpPort(port));
        inst.setCompletedNum(0);
        inst.setWaitNum(0);
        scheduleInstanceDao.insertSelective(inst);

        Map<String, Object> condMap = new HashMap<>();
        condMap.put("instanceId", instanceId);
        List<ScheduleInstanceDtl> instTaskList = scheduleInstanceDtlDao.getListByMap(condMap);

        for (ScheduleInstanceDtl instDtlOld : instTaskList) {
            ScheduleInstanceDtl instanceDtl = new ScheduleInstanceDtl();
            instanceDtl.setId(null);
            instanceDtl.setInstanceId(newInstanceId);
            instanceDtl.setRunStatus(1);
            instanceDtl.setUpdateTime(new Date());

            instanceDtl.setParams(instDtlOld.getParams());
            instanceDtl.setWorkItemId(instDtlOld.getWorkItemId());
            instanceDtl.setWorkItemName(instDtlOld.getWorkItemName());
            instanceDtl.setWorkItemId(instDtlOld.getWorkItemId());
            instanceDtl.setWorkItemName(instDtlOld.getWorkItemName());
            instanceDtl.setTotalNum(instDtlOld.getTotalNum());
            instanceDtl.setSuccNum(0);
            instanceDtl.setErrNum(0);
            scheduleInstanceDtlDao.insertSelective(instanceDtl);
        }
    }

    @Override
    public void completedInst(Long[] ids) {
        for (Long id : ids) {
            this.completedInst(id);
        }
    }

    /**
     * 更新为 执行完成
     *
     * @param id
     */
    @Transactional
    public void completedInst(Long id) {
        ScheduleInstance updateInst = new ScheduleInstance();
        updateInst.setId(id);
        updateInst.setUpdateTime(new Date());
        updateInst.setStatus(4);
        updateInst.setRemark("手工处理为[执行完成]");
        scheduleInstanceDao.updateByPrimaryKeySelective(updateInst);

        ScheduleInstance inst = scheduleInstanceDao.selectByPrimaryKey(id);
        Map<String, Object> condMap = new HashMap<>();
        condMap.put("instanceId", inst.getInstanceId());
        List<ScheduleInstanceDtl> instDtlList = scheduleInstanceDtlDao.getListByMap(condMap);

        for (ScheduleInstanceDtl instanceDtl : instDtlList) {
            ScheduleInstanceDtl updateInstdtl = new ScheduleInstanceDtl();
            updateInstdtl.setId(instanceDtl.getId());
            updateInstdtl.setRunStatus(4);
            updateInstdtl.setUpdateTime(new Date());
            scheduleInstanceDtlDao.updateByPrimaryKeySelective(updateInstdtl);
        }
    }
}
